/**
 * @desc
 * 自动对-p组件 注入 组件 名命空间信息..
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/11/21
 **/
'use strict';
let widetUtil =require('@qianmi/x-site-common/util/widget-util');
let fileUtil =require('@qianmi/x-site-common/util/file-util');
var path = require('path');
let _flag  ='__XSite_widgetInject';

let _workBench = path.join(__dirname,'../../../../');

module.exports = function(source) {
  console.log('widget-pre-loader');
  if(source.indexOf(_flag) > 0 ){
    
    // let  _topDir = this.resourcePath.split(_util.getWorbenchName())[0];
    let reqNs  = this.resourcePath.replace('.js','').replace(_workBench,'').replace(/\\/g, '/');
    
    console.log(reqNs,_workBench,this.resourcePath);
    let options  ={};
    if(reqNs.endsWith('-p')){
      reqNs=reqNs.substring(0,reqNs.length-2);
      options.isProxy =true;
    }
    let widgetNormalName =widetUtil.extraWidgetNameFromPath(reqNs);
    options.widgetName =widgetNormalName;
    options.widgetNameSpace = reqNs;
    
    let _begIndex  =source.indexOf(_flag)+_flag.length;
    let _endIndex = source.indexOf(")",_begIndex);
    
    let _preContent  =source.substring(0,_endIndex);
    let _afterContent  =source.substring(_endIndex);
    
    let result =_preContent+`,${JSON.stringify(options)}`+_afterContent;//自动注入后缀参数,便于随后添加参数;;;
    return result;
    
  }else {
    return source;
  }
}